home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / misc / update / tpasc302.cpt / TCL Update / New Starter Files / CStarterDoc.p
Encoding:
Text File  |  1990-05-11  |  12.9 KB  |  536 lines

  1. {****************************************************}
  2. {}
  3. {        CStarterDoc.p                                                                    }
  4. {}
  5. {        Document methods for a typical application.                                    }
  6. {}
  7. {        Copyright ⌐ 1989, Symantec Corporation.  All rights reserved.            }
  8. {}
  9. {****************************************************}
  10.  
  11.  
  12. unit CStarterDoc;
  13.  
  14. interface
  15.  
  16.     uses
  17.         TCL, StarterIntf;
  18.  
  19. implementation
  20.  
  21.  
  22. {**}
  23. { * IStarterDoc}
  24. { *}
  25. { *    This is your document's initialization method.}
  26. { *    If your document has its own instance variables, initialize}
  27. { *    them here.}
  28. { *}
  29. { *    The least you need to do is initialize the superclass.}
  30. { *}
  31. { **}
  32.  
  33.     procedure CStarterDoc.IStarterDoc (aSupervisor: CApplication; printable: Boolean);
  34.  
  35.     begin
  36.         IDocument(aSupervisor, printable);
  37.     end;
  38.  
  39.  
  40. {**}
  41. { * Free}
  42. { *}
  43. { *    This is your document's destruction method.}
  44. { *    If you allocated memory in your initialization method}
  45. { *    or opened temporary files, this is the place to release them.}
  46. { *}
  47. { *    Be sure to call the inherited method!}
  48. { *}
  49. { **}
  50.  
  51.     procedure CStarterDoc.Free;
  52.  
  53.     begin
  54.         inherited Free;
  55.     end;
  56.  
  57.  
  58. {**}
  59. { * DoCommand}
  60. { *}
  61. { *    In this method, you handle all the commands your document}
  62. { *    deals with.}
  63. { *}
  64. { *    Be sure to call the inherited method to handle the standard}
  65. { *    document commands: cmdClose, cmdSave, cmdSaveAs, cmdRevert,}
  66. { *    cmdPageSetup, cmdPrint, and cmdUndo. To change the way these}
  67. { *    commands are handled, override the appropriate methods instead}
  68. { *    of handling them here.}
  69. { *}
  70. { **}
  71.  
  72.     procedure CStarterDoc.DoCommand (theCommand: longint);
  73.  
  74.     begin
  75.         case theCommand of
  76.  
  77.         { Your document commands go here (dummyCmd is given as an example)    }
  78.  
  79.             dummyCmd: 
  80.                 begin
  81.             { code for dummyCmd }
  82.                 end;
  83.  
  84.             otherwise
  85.                 inherited DoCommand(theCommand);
  86.         end;
  87.     end;
  88.  
  89. {**}
  90. { * UpdateMenus}
  91. { *}
  92. { *    In this method you can enable menu commands that apply when}
  93. { *    your document is active.}
  94. { *}
  95. { *    Be sure to call the inherited method to get the default behavior.}
  96. { *    The inherited method enables these commands: cmdClose, cmdSaveAs,}
  97. { *    cmdSave, cmdRevert, cmdPageSetup, cmdPrint, cmdUndo.}
  98. { *}
  99. { **}
  100.  
  101.     procedure CStarterDoc.UpdateMenus;
  102.  
  103.     begin
  104.         inherited UpdateMenus;
  105.  
  106.     { Enable your menu commands here (enable each one with a call to    }
  107.     { gBartender.EnableCmd(command_number)).                            }
  108.  
  109.     end;
  110.  
  111.  
  112. {**}
  113. { * NewFile}
  114. { *}
  115. { *    When the user chooses New from the File menu, the CreateDocument}
  116. { *    method in your Application class will send a newly created document}
  117. { *    this message. This method needs to create a new window, ready to}
  118. { *    work on a new document.}
  119. { *}
  120. { *    Since this method and the OpenFile method share the code for creating}
  121. { *    the window, you should use an auxiliary window-building method.}
  122. { *}
  123. { **}
  124.  
  125.     procedure CStarterDoc.NewFile;
  126.         var
  127.             wTitle: Str255;                { Window title string                }
  128.             wCount: integer;                { Index number of new window    }
  129.             wNumber: Str255;                { Index number as a string        }
  130.  
  131.     begin
  132.         {*}
  133. {         **    BuildWindow is the method that}
  134. {         **    does the work of creating a window.}
  135. {         **    Its parameter should be a handle to the data that}
  136. {         **    you want to display in the window.}
  137. {         **    Since this is a new window, there's nothing}
  138. {         **    to display.}
  139. {         **}
  140. {         *}
  141.  
  142.         BuildWindow(nil);
  143.  
  144.         {*}
  145. {         **    Append an index number to the}
  146. {         **    default name of the window.}
  147. {         *}
  148.  
  149.         itsWindow.GetTitle(wTitle);
  150.         wCount := gDecorator.GetWCount;
  151.         NumToString(wCount, wNumber);
  152.         wTitle := concat(wTitle, ' ');
  153.         wTitle := concat(wTitle, wNumber);
  154.         itsWindow.SetTitle(wTitle);
  155.  
  156.         {*}
  157. {         **    Send the window a Select message to make}
  158. {         **    it the active window.}
  159. {         *}
  160.  
  161.         itsWindow.Select;
  162.     end;
  163.  
  164.  
  165. {**}
  166. { * OpenFile}
  167. { *}
  168. { *    When the user chooses Open╔ from the File menu, the OpenDocument}
  169. { *    method in your Application class will let the user choose a file}
  170. { *    and then send a newly created document this message. The information}
  171. { *    about the file is in the SFReply record.}
  172. { *}
  173. { *    In this method, you need to open the file and display its contents}
  174. { *    in a window. This method uses the auxiliary window-building method.}
  175. { *}
  176. { **}
  177.  
  178.     procedure CStarterDoc.OpenFile (macSFReply: SFReply);
  179.  
  180.         var
  181.             theFile: CDataFile;
  182.             theData: Handle;
  183.             theName: Str255;
  184.             theError, temp: OSErr;
  185.             tempBool: Boolean;
  186.  
  187.     begin
  188.         {*}
  189. {         **     Create a file and send it an SFSpecify}
  190. {         **    message to set up the name, volume, and}
  191. {         **    directory.}
  192. {         **}
  193. {         *}
  194.  
  195.         new(theFile);
  196.         theFile.IDataFile;
  197.         theFile.SFSpecify(macSFReply);
  198.  
  199.         {*}
  200. {         **    Be sure to set the instance variable}
  201. {         **    so other methods can use the file if they}
  202. {         **    need to. This is especially important if}
  203. {         **    you leave the file open in this method.}
  204. {         **    If you close the file after reading it, you}
  205. {         **    should be sure to set itsFile to nil.}
  206. {         **}
  207. {         *}
  208.  
  209.         itsFile := theFile;
  210.  
  211.         {*}
  212. {         **    Send the file an Open message to}
  213. {         **    open it. You can use the ReadSome or}
  214. {         **    ReadAll methods to get the contents of the file.}
  215. {         **}
  216. {         *}
  217.  
  218.         theError := theFile.Open(fsRdWrPerm);
  219.  
  220.         {*}
  221. {         **    Check to see if we were able to open}
  222. {         **    the file. Send the error handler}
  223. {         **    a CheckOSError message. If there was}
  224. {         **    an error, CheckOSError returns false}
  225. {         **    and reports the error in an alert.}
  226. {         **    The default error message displays the}
  227. {         **    error number.}
  228. {         **    You can use Estr resources to customize}
  229. {         **    the error message.}
  230. {         **}
  231. {         **    Note that we send ourselves a Free}
  232. {         **    message. Since we're not going to open,}
  233. {         **    we should get rid of the object.}
  234. {         *}
  235.  
  236.         if not gError.CheckOSError(theError) then
  237.             begin
  238.                 Free;
  239.                 Exit(OpenFile);
  240.             end;
  241.  
  242.         {*}
  243. {         **    Make sure that the memory request to read}
  244. {         **    the data from the file doesn't use up any}
  245. {         **    of our rainy day fund and that the GrowMemory}
  246. {         **    method (in the application) knows that it's OK}
  247. {         **    if we couldn't get enough memory.}
  248. {         **}
  249. {         *}
  250.  
  251.         gApplication.RequestMemory(FALSE, TRUE);
  252.         temp := theFile.ReadAll(theData);                    { ReadAll creates the handle         }
  253.         gApplication.RequestMemory(FALSE, FALSE);    { Reset canFail to FALSE for         }
  254.                                                             { default memory-error handling }
  255. {}
  256. {}
  257. {        {*}
  258. {         **    If there isn't enough memory to open,}
  259. {         **    post the error (should be -108, memFullErr)}
  260. {         **     and get rid of ourselves.}
  261. {         **}
  262. {         *}
  263.  
  264.         if theData = nil then
  265.             begin
  266.                 tempBool := gError.CheckOSError(MemError);
  267.                 Free;
  268.                 Exit(OpenFile);
  269.             end;
  270.  
  271.  
  272.         BuildWindow(theData);
  273.  
  274.         {*}
  275. {         **    In your application, you'll probably store}
  276. {         **    the data in some form as an instance variable}
  277. {         **    in your document class. For this example, there's}
  278. {         **    no need to save it, so we'll get rid of it.}
  279. {         **}
  280. {         *}
  281.  
  282.         DisposHandle(theData);
  283.  
  284.         {*}
  285. {         **    In this implementation, we leave the file}
  286. {         **    open. You might want to close it after}
  287. {         **    you've read in all the data.}
  288. {         **}
  289. {         *}
  290.  
  291.         itsFile.GetName(theName);
  292.         itsWindow.SetTitle(theName);
  293.         itsWindow.Select;            { Don't forget to make the window active }
  294.     end;
  295.  
  296.  
  297.  
  298. {**}
  299. { * BuildWindow}
  300. { *}
  301. { *    This is the auxiliary window-building method that the}
  302. { *    NewFile and OpenFile methods use to create a window.}
  303. { *}
  304. { *    In this implementation, the argument is a handle to }
  305. { * the data to display.}
  306. { *}
  307. { **}
  308.  
  309.     procedure CStarterDoc.BuildWindow (theData: Handle);
  310.  
  311.         var
  312.             theScrollPane: CScrollPane;
  313.             theMainPane: CStarterPane;
  314.             theWindow: CWindow;         { Altered by TCL Weaver 1.0 (5/9/90) }
  315.  
  316.     begin
  317.         {*}
  318. {         **    First create the window and initialize}
  319. {         **    it. The first argument to IWindow is the resource ID}
  320. {         **    of the window. The second argument specifies}
  321. {         **    whether the window is a floating window.}
  322. {         **    The third argument is the window's enclosure; it}
  323. {         **    should always be gDesktop. The last argument is}
  324. {         **    the window's supervisor in the Chain of Command;}
  325. {         **    it should always be the Document object.}
  326. {         **}
  327. {         *}
  328.  
  329.         new(theWindow);         { Altered by TCL Weaver 1.0 (5/9/90) }
  330.         itsWindow := theWindow;
  331.         itsWindow.IWindow(WINDStarter, FALSE, gDesktop, SELF);
  332.  
  333.         {*}
  334. {         **    After you create the window, you can use the}
  335. {         **    SetSizeRect message to set the window╒s maximum}
  336. {         **    and minimum size. Be sure to set the max & min}
  337. {         **    BEFORE you send a PlaceNewWindow message to the}
  338. {         **    decorator.}
  339. {         **}
  340. {         **     The default minimum is 100 by 100 pixels. The}
  341. {         **    default maximum is the bounds of GrayRgn (the}
  342. {         **    entire display area on all screens.)}
  343. {         **}
  344. {         **    We'll use the defaults.}
  345. {         **}
  346. {         *}
  347.  
  348. {         *}
  349. {         **    Our window will contain a ScrollPane, }
  350. {         **    which in turn will contain a Panorama.}
  351. {         **    Now, let's create the ScrollPane.}
  352. {         *}
  353.  
  354.  
  355.         new(theScrollPane);
  356.  
  357.         {*}
  358. {         **    There are two ways to initialize a scroll pane:}
  359. {         **        1. You can specify all the values}
  360. {         **           right in your code, as we do in this example.}
  361. {         **        2. You can create a ScPn resource and}
  362. {         **           initialize the pane from the information}
  363. {         **           in the resource.}
  364. {         **}
  365. {         *}
  366.  
  367.         theScrollPane.IScrollPane(itsWindow, SELF, 10, 10, 0, 0, sizELASTIC, sizELASTIC, TRUE, TRUE, TRUE);
  368.  
  369.         {*}
  370. {         **    The FitToEnclFrame method makes the}
  371. {         **    scroll pane be as large as its enclosure.}
  372. {         **    In this case, the enclosure is the window,}
  373. {         **    so the scroll pane will take up the entire}
  374. {         **    window.}
  375. {         **}
  376. {         *}
  377.  
  378.         theScrollPane.FitToEnclFrame(TRUE, TRUE);
  379.  
  380.  
  381.         {*}
  382. {         **    itsMainPane is the document's focus}
  383. {         **    of attention. Some of the standard}
  384. {         **    classes (particularly CPrinter) rely}
  385. {         **    on itsMainPane pointing to the main}
  386. {         **    pane of your window.}
  387. {         **}
  388. {         **    itsGopher specifies which object}
  389. {         **    should become the gopher when}
  390. {         **    the document becomes active. By default,}
  391. {         **    the document becomes the gopher. It╒s}
  392. {         **    likely that your main pane handles commands}
  393. {         **    so you╒ll almost always want to set itsGopher}
  394. {         **    to point to the same object as itsMainPane.}
  395. {         **}
  396. {         **    Note that the main pane is the}
  397. {         **    panorama in the scroll pane and not}
  398. {         **    the scroll pane itself.}
  399. {         **}
  400. {         *}
  401.  
  402.         new(theMainPane);
  403.         theMainPane.IStarterPane(theScrollPane, SELF, 0, 0, 0, 0, sizELASTIC, sizELASTIC);
  404.         itsMainPane := theMainPane;
  405.         itsGopher := theMainPane;
  406.  
  407.         {*        The FitToEnclosure method makes the pane}
  408. {         **    fit inside the enclosure. The inside (or}
  409. {         **    interior) of a scroll pane is defined as}
  410. {         **    the area inside the scroll bars.}
  411. {         *}
  412.  
  413.         theMainPane.FitToEnclosure(TRUE, TRUE);
  414.  
  415.         {*}
  416. {         **    Send the scroll pane an InstallPanorama message}
  417. {         **    to associate the panorama with the scroll pane.}
  418. {         **}
  419. {         *}
  420.  
  421.         theScrollPane.InstallPanorama(theMainPane);
  422.  
  423.         {*}
  424. {         **    The Decorator is a global object that takes care}
  425. {         **    of placing and sizing windows on the screen.}
  426. {         **    You don't have to use it.}
  427. {         **}
  428. {         *}
  429.  
  430.         gDecorator.PlaceNewWindow(itsWindow);
  431.     end;
  432.  
  433.  
  434. {**}
  435. { * DoSave}
  436. { *}
  437. { *    This method handles what happens when the user chooses Save from the}
  438. { *    File menu. This method should return TRUE if the file save was successful.}
  439. { *    If there is no file associated with the document, you should send a}
  440. { *    DoSaveFileAs message.}
  441. { *}
  442. { **}
  443.  
  444.     function CStarterDoc.DoSave: Boolean;
  445.  
  446.     begin
  447.         {*}
  448. {         **    If you closed your file in your NewFile method,}
  449. {         **    you'll need a different way than this to determine}
  450. {         **    if there's a file associated with your document.}
  451. {         **}
  452. {         *}
  453.  
  454.         if itsFile = nil then
  455.             DoSave := DoSaveFileAs
  456.         else
  457.             begin
  458.  
  459.         {*}
  460. {         **    In your application, this is where you'd}
  461. {         **    write out your file. If you left it open,}
  462. {         **    send the WriteSome or WriteAll message}
  463. {         **    to itsFile.}
  464. {         **}
  465. {         *}
  466.  
  467.                 dirty := FALSE;                    { Document is no longer dirty        }
  468.                 gBartender.DisableCmd(cmdSave);
  469.                 DoSave := TRUE;                    { Save was successful                }
  470.             end;
  471.     end;
  472.  
  473.  
  474. {**}
  475. { * DoSaveAs}
  476. { *}
  477. { *    This method handles what happens when the user chooses Save As╔ from the}
  478. { *    File menu. The default DoCommand method for documents sends a DoSaveFileAs}
  479. { *    message which displays a standard put file dialog and sends this message.}
  480. { *    The SFReply record contains all the information about the file you're about}
  481. { *    to create.}
  482. { *}
  483. { **}
  484.  
  485.     function CStarterDoc.DoSaveAs (macSFReply: SFReply): Boolean;
  486.         var
  487.             temp: OSErr;
  488.             theFile: CFile; { Altered by TCL Weaver 1.0 (5/9/90) }
  489.  
  490.     begin
  491.         {*}
  492. {         **    If there's a file associated with this document}
  493. {         **    already, close it. The Free method for files}
  494. {         **    sends a Close message to the file before releasing}
  495. {         **    its memory.}
  496. {         **}
  497. {         *}
  498.  
  499.         if itsFile <> nil then
  500.             itsFile.Free;
  501.  
  502.  
  503.         {*}
  504. {         **    Create a new file, and then save it normally.}
  505. {         **}
  506. {         *}
  507.  
  508.         new(CDataFile(theFile));        { Altered by TCL Weaver 1.0 (5/9/90) }
  509.         itsFile := theFile;
  510.         CDataFile(itsFile).IDataFile;
  511.         itsFile.SFSpecify(macSFReply);
  512.         temp := itsFile.CreateNew(gSignature, 'TEXT');
  513.         temp := itsFile.Open(fsRdWrPerm);
  514.  
  515.         itsWindow.SetTitle(macSFReply.fName);
  516.  
  517.         DoSaveAs := DoSave;
  518.     end;
  519.  
  520.  
  521. {**}
  522. { * DoRevert}
  523. { *}
  524. { *    If your application supports the Revert command, this method}
  525. { *    should close the current file (without writing anything out)}
  526. { *    and read the last saved version of the file.}
  527. { *}
  528. { **}
  529.  
  530.     procedure CStarterDoc.DoRevert;
  531.  
  532.     begin
  533.     end;
  534.  
  535.  
  536. end.